package com.orangelife.mobile.hourlyEmployee.adapter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.confirmOrder.activity.PayConfirmOrderActivity;
import com.orangelife.mobile.hourlyEmployee.activity.OrderDetailActivity;
import com.orangelife.mobile.pay.biz.PaymentModule;
import com.orangelife.mobile.util.DateUtil;
import com.orangelife.mobile.util.StringUtil;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class OrderListAdapter<T> extends FatherAdapter<T> {

	private Context context;
	private List<Map<String, Object>> list;
	private Map<String, Object> infomap;
	private Dialog dialog;
	private Map<String,Object> mapD;
	
	public OrderListAdapter(Context context,
			List<Map<String, Object>> list){
		this.context = context;
		this.list = list;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_lv_hourly_employee, null);
			holder.tvStatus = (TextView)convertView.findViewById(R.id.tvStatus);
			holder.tvTime = (TextView)convertView.findViewById(R.id.tvTime);
			holder.tvAddress = (TextView)convertView.findViewById(R.id.tvAddress);
			holder.btnOrderConfirm = (Button)convertView.findViewById(R.id.btnConfirmOrder);
			holder.llClick = (LinearLayout)convertView.findViewById(R.id.llClick);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		infomap = list.get(position);
		String statusStr = infomap.get("status").toString();
		int stauts = 0;
		if(!StringUtil.isBlank(statusStr)){
			stauts = Integer.parseInt(statusStr);
		}
		switch (stauts) {
		case 1:
			statusStr = "受理中";
			
			break;
		case 2:
			statusStr = "待上门";
			
			break;
		case 3:
			statusStr = "已取消";
			
			break;
		case 4:
			statusStr = "待取消";
			
			break;
		case 5:
			statusStr = "已完成";
			
			break;

		default:
			break;
		}
		holder.tvStatus.setText(statusStr);
		DateUtil dateUtil = new DateUtil();
		String time = dateUtil.tranStringForDate_33(infomap.get("serviceDate")
				.toString());
		holder.tvTime.setText(time+" "+infomap.get("hours").toString()+"小时");
		holder.tvAddress.setText(infomap.get("shipAddr").toString());
		if(stauts == 2){
			holder.btnOrderConfirm.setVisibility(View.VISIBLE);
		}else{
			holder.btnOrderConfirm.setVisibility(View.GONE);
		}
		holder.llClick.setOnClickListener(new listener(infomap));
		holder.btnOrderConfirm.setOnClickListener(new listener(infomap));
		
		return convertView;
	}
	
	class listener implements View.OnClickListener {

		private Map<String, Object> map = null;

		public listener(Map<String, Object> map) {
			this.map = map;
		}

		@Override
		public void onClick(View view) {
			Intent intent = new Intent();
			switch (view.getId()) {
			case R.id.llClick:
				if (!NetworkUtil.isConnectedNet(context)) {
					Toast.makeText(
							context,
							context.getResources().getString(
									R.string.net_disconnected),
							Toast.LENGTH_SHORT).show();
					return;
				}
					intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putSerializable("detail", (Serializable) map);
					intent.setClass(context, OrderDetailActivity.class);
					intent.putExtras(bundle);
					context.startActivity(intent);
				break;
			case R.id.btnConfirmOrder:
				mapD = map;
				isOrderDialog();
				break;

			default:
				break;
			}

		}

		/**
		 * 弹出对话框
		 */
		private void isOrderDialog() {
			final ConfirmDialog dialog = new ConfirmDialog(context, "温馨提示", context.getResources().getString(R.string.confirm_service_complete), "返回", context.getResources().getString(R.string.sure));
			dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
				@Override
				public void doLeft() {
					dialog.dismiss();
				}

				@Override
				public void doRight() {
					dialog.dismiss();
					//确定订单
					Intent intent = new Intent();
					intent.putExtra("orderInfo", "钟点工服务"+mapD.get("price").toString()+"(x"+mapD.get("hours").toString()+"小时)");
					intent.putExtra("Payment", mapD.get("totalCost").toString());
					intent.putExtra(PaymentModule.PAID_METHOD,PaymentModule.mode[8]);
					intent.putExtra("serviceOrderID", mapD.get("serviceOrderID").toString());
					intent.setClass(context, PayConfirmOrderActivity.class);
					context.startActivity(intent);
					return;
				}
			});

			dialog.show();
		}
		private void isOrderDialog1() {
			AlertDialog.Builder builder = new Builder(context);
			builder.setMessage(context.getResources().getString(R.string.confirm_service_complete));
			builder.setPositiveButton(context.getResources().getString(R.string.sure), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					//确定订单
					Intent intent = new Intent();
					intent.putExtra("orderInfo", "钟点工服务"+mapD.get("price").toString()+"(x"+mapD.get("hours").toString()+"小时)");
					intent.putExtra("Payment", mapD.get("totalCost").toString());
					intent.putExtra(PaymentModule.PAID_METHOD,PaymentModule.mode[8]);
					intent.putExtra("serviceOrderID", mapD.get("serviceOrderID").toString());
					intent.setClass(context, PayConfirmOrderActivity.class);
					context.startActivity(intent);
					return;
				}
			});
			builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
		}
	}
	
	class ViewHolder {
		/** 状态 */
		private TextView tvStatus;
		/** 时间 */
		private TextView tvTime;
		/** 地址 */
		private TextView tvAddress;
		/** 确认订单按钮 */
		private Button btnOrderConfirm;
		/** 详情点击 */
		private LinearLayout llClick;
	}

	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return OrderListAdapter.this;
	}

}
